var goods = new Object;  //购物车
var user = new Object;  //用户
var Ajax = new Object;

/* *
* 调用此方法发送HTTP请求。
*
* @public
* @param   {string}    url             请求的URL地址
* @param   {mix}       params          发送参数
* @param   {Function}  callback        回调函数
* @param   {string}    ransferMode     请求的方式，有"GET"和"POST"两种
* @param   {string}    responseType    响应类型，有"JSON"、"XML"和"TEXT"三种
*/
Ajax.call = function(url, params, callback, transferMode, responseType) {
    $.ajax({
        type: transferMode,
        url: url,
        data: params,
        success: callback,
        dataType: responseType,
        cache: false
    });
}
//添加收藏夹
goods.AddFavorite = function(goodsId) {
    Ajax.call('http://183.57.44.17/tools/ajax.aspx', { act: 'AddFavorite', goods_id: goodsId }, function(data) {
        alert(data.message);
    }, 'POST', 'json');
}
//删除收藏夹
goods.deleteFavorite = function(favorite_id) {
    Ajax.call('http://183.57.44.17/tools/ajax.aspx', { act: 'deleteFavorite', id: favorite_id }, function(data) {
        window.top.location.reload();
    }, 'POST', 'json');
}

//退出登录
user.Logout = function() {
    Ajax.call('http://183.57.44.17/login.aspx', { act: 'logout' }, function(data) {
        if (data.error == 1) {
            alert(data.message);
        }
        else {
            alert(data.message);
            window.top.location.reload();
        }
    }, 'POST', 'json');
}

//ajax提交
function AjaxSubmit(form) {
    $(form).ajaxSubmit({
        dataType: 'json',
        resetForm: true,
        success: function(data) {
            if (data.message != "") {
                alert(data.message);
            }

            if (data.error == 0) {
                if (typeof (url) != "undefined") {
                    window.location.href = url;
                }
            }
        }
    });
    return false;
}

var res = false;
function FormvSubmit(form) {
    $(form).find(".sbtn").attr("disabled", false);
    $(form).submit(function() { if (res) { AjaxSubmit(form); res = false; } return false; });
}


function ReloadCaptcha() {
    var s = new Date();
    $('.captcha').attr('src', '/tools/verifyimagepage.aspx?' + s.getMilliseconds());
}

/*添加到收藏夹*/
function addFavorite(val) {
    if (document.all) {
        window.external.addFavorite(window.location, val);
    }
    else if (window.sidebar) {
        window.sidebar.addPanel(val, window.location, "");
    }
}


/*图片切换显示 */
function changeImage(tagname, i) {
    var tag = tagname + i;
    document.getElementById("infoImg").src = document.getElementById(tag).src.replace("ico_", "info_");
    document.getElementById("originalImg").href = document.getElementById(tag).src.replace("ico_", "_");
}

/*单页分页*/
function go_desc_page(page_index, page_count) {
    for (var i = 0; i < page_count; i++) {
        if (i + 1 == page_index) {
            document.getElementById("desc_page" + (i + 1)).style.display = "";
            document.getElementById("desc_page_link" + (i + 1)).className = "desc_page_link_cur";
        } else {
            document.getElementById("desc_page" + (i + 1)).style.display = "none";
            document.getElementById("desc_page_link" + (i + 1)).className = "desc_page_link";
        }
    }
}


//产品搜索
function pr_search() {
    var url = "";
    var key = document.getElementById('pr_key').value;
    if (key == '' || key == '输入搜素关键词') {
        alert('输入搜素关键词');
        return;
    }
    url = "/search/k_" + encodeURI(key) + ".aspx";
    window.location.href = url;
    
}
//function email() {
//    var s_key = jQuery.trim($("#e_key").val());
//    var filter=/^.+@.+\..{2,3}$/;
//    if (filter.test(s_key)){
//        AjaxSubmit("#top_ding");
//    }    
//    else{
//        alert("请输入E-mail订阅最新资讯！");
//        $("#e_key").focus();
//    }
//    return false;
//}

//加入收藏
function favorite(sURL, sTitle) {
    try {
        window.external.addFavorite(sURL, sTitle);
    } catch (e) {
        try {
            window.sidebar.addPanel(sTitle, sURL, "");
        } catch (e) {
            alert("加入收藏失败，请按快捷键Ctrl+D进行添加");
        }
    }
}

//登录
function login() {
    var user_name = document.getElementById("user_name").value;
    var password = document.getElementById("password").value;
    var captcha = document.getElementById("captcha").value;
    var reg1 = /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/;
    if (user_name == "") { alert("请输入用户名！"); return; } else {
    if (!reg1.test(user_name)) { alert("请输入正确的E-mail！"); return; }
    }
    if (password == "") { alert("请输入密码！"); return; }
    if (captcha == "") { alert("请输入验证码！"); return; }
   
    $.ajax({
        type: 'POST',
        url: '../../login.aspx-act=login.htm'/*tpa=http://183.57.44.17/login.aspx?act=login*/,
        data: { act: 'login', user_name: user_name, password: password, captcha: captcha },
        success: function(data) { alert(data.message); if (data.error == 0) { $("#form").get(0).reset(); location.href = "../../user-login.aspx.htm"/*tpa=http://183.57.44.17/user.aspx?act=index*/; } },
        dataType: 'json',
        cache: false
    });
}

//注册
function region() {
    var user_name = document.getElementById("user_name").value;
    var password = document.getElementById("password").value;
    var captcha = document.getElementById("captcha").value;
    var password_confirm = document.getElementById("password_confirm").value;
    var name = document.getElementById("name").value;
  
    var reg1 = /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/;

    if (user_name == "") { alert("请输入用户名！"); return; }else {
    if (!reg1.test(user_name)) { alert("请输入正确的E-mail！"); return; }
        }
    if (password == "") { alert("请输入密码！"); return; }
    if (password_confirm == "") { alert("请输入确认密码！"); return; } else { if (password != password_confirm) { alert("两次输入的密码不一致！"); return; } }
    
    if (captcha == "") { alert("请输入验证码！"); return; }
    if (name == "") { alert("请输入昵称！"); return; }

    $.ajax({
        type: 'POST',
        url: '../../login.aspx-act=RegCheck.htm'/*tpa=http://183.57.44.17/login.aspx?act=RegCheck*/,
        data: { act: 'RegCheck', user_name: user_name, password: password, captcha: captcha, name: name },
        success: function(data) { alert(data.message); if (data.error == 0) { $("#form").get(0).reset(); location.href = "../../user-login.aspx.htm"/*tpa=http://183.57.44.17/user.aspx?act=index*/; } },
        dataType: 'json',
        cache: false
    });
}

function zq_onclick(t_val, v_id) {
    if (document.getElementById(v_id).value == t_val) {
        $("#" + v_id).val('');
    }
}

//拷贝网址
function Copy() {
    var clipBoardContent = this.location.href;
    window.clipboardData.setData("Text", clipBoardContent);
    alert("复制成功!");
}

//打印页面
function preview(oper) {
    if (oper < 10) {
        bdhtml = window.document.body.innerHTML; //获取当前页的html代码  
        sprnstr = "<!--startprint" + oper + "-->"; //设置打印开始区域  
        eprnstr = "<!--endprint" + oper + "-->"; //设置打印结束区域  
        prnhtml = bdhtml.substring(bdhtml.indexOf(sprnstr) + 18); //从开始代码向后取html  

        prnhtmlprnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr)); //从结束代码向前取html  
        window.document.body.innerHTML = prnhtml;
        window.print();
        window.document.body.innerHTML = bdhtml;
    } else {
        window.print();
    }
}

//文字大小
function fontsize(b_id,size) {
    document.getElementById(b_id).style.fontSize = size + 'px'
}

//设为首页 <a onclick="SetHome(this,window.location)">设为首页</a>
function SetHome(obj, vrl) {
  
    try {
        obj.style.behavior = 'url(#default#homepage)'; obj.setHomePage(vrl);
    }
    catch (e) {
        if (window.netscape) {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e) {
                alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");
            }
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage', vrl);
        }
    }
}


function add_dingdan() {  //jquery获取复选框值

    var s = '';
    var c = 0;
    $('input[name="checkbox"]:checked').each(function() {
        s += $(this).val() + ',';
    });

    if (s.length > 0) {
        //得到选中的checkbox值序列
        s = s.substring(0, s.length - 1);
    } else {
    alert(s == '' ? '你还没有选择任何内容！' : s);
    return false;
    }
    var goods_name = $("#goods_name").val();
    var name = $("#name").val();
    var tel = $("#tel").val();
    var address = $("#address").val();
    var goods_id = $("#goods_id").val();
    var peisong = $("#peisong").val();
    var contact = $("#contact").val();
    var str2 = $("#str2").val();
    
    if (name == "") {
        alert("请输入联系人");
        return false;
     }

     if (tel == "") {
         alert("请输入电话");
         return false;
     }

     if (address == "") {
         alert("请输入地址");
         return false;
     }
    
    
    $.ajax({
        type: 'POST',
        url: 'http://183.57.44.17/tools/ajax.aspx?act=add_order',
        data: { act: 'add_order', name: name, tel: tel, address: address, goods_id: goods_id, peisong: peisong, zs: s, contact: contact, goods_name: goods_name, str2: str2 },
        success: function(data) { alert(data.message); if (data.error == 0) { $("#form1").get(0).reset(); } },
        dataType: 'json',
        cache: false
    });
}

function add_join() {

    var user_email = $("#user_email").val();
    var str1 = $("#str1").val();
    var user_name = $("#user_name").val();
    var address = $("#address").val();
    var tel = $("#tel").val();
    var msg_content = $("#msg_content").val();

    if (user_email == "") {
        alert("请输入邮箱");
        return false;
    }

    if (str1 == "") {
        alert("请输入公司名称");
        return false;
    }
    
    if (user_name == "") {
        alert("请输入联系人");
        return false;
    }

    if (tel == "") {
        alert("请输入电话");
        return false;
    }

    if (address == "") {
        alert("请输入地址");
        return false;
    }


    $.ajax({
        type: 'POST',
        url: 'http://183.57.44.17/tools/ajax.aspx?act=add_join',
        data: { act: 'add_join', user_email: user_email, str1: str1, user_name: user_name, address: address, tel: tel, msg_content: msg_content},
        success: function(data) { alert(data.message); if (data.error == 0) { $("#form1").get(0).reset(); } },
        dataType: 'json',
        cache: false
    });
}

function add_feedback() {

    var user_email = $("#user_email").val();
    var user_company = $("#user_company").val();
    var user_name = $("#user_name").val();
    var user_tel = $("#user_tel").val();
    var msg_content = $("#msg_content").val();

    if (user_email == "") {
        alert("请输入邮箱");
        return false;
    }

    if (user_company == "") {
        alert("请输入公司名称");
        return false;
    }

    if (user_name == "") {
        alert("请输入联系人");
        return false;
    }

    if (user_tel == "") {
        alert("请输入电话");
        return false;
    }


    $.ajax({
        type: 'POST',
        url: 'http://183.57.44.17/tools/ajax.aspx?act=feedback',
        data: { act: 'feedback', user_email: user_email, user_company: user_company, user_name: user_name, user_tel: user_tel, msg_content: msg_content },
        success: function(data) { alert(data.message); if (data.error == 0) { $("#form1").get(0).reset(); } },
        dataType: 'json',
        cache: false
    });
}